log using "owenli_log.log", append

// Replication files for "The Conditional Nature of Publication Bias: The Conditional Nature of Publication Bias: A Meta-Regression Analysis" 
	// by Erica Owen and Quan Li
// Date: 1/6/2019
// File author: Erica Owen (ericaowen@pitt.edu)
// This do file replicates results in supplemental appendix

// Setwd


// #0
// program set-up
version 13.1
clear all
macro drop _all
set linesize 80


// Load data
use "owenli_data.dta", clear


// Table A1. Summary by study


sort studyid
by studyid: egen meant=mean(tstat)
by studyid: egen mint=min(tstat)
by studyid: egen maxt=max(tstat)
by studyid: egen minyr=min(tstart)
by studyid: egen maxyr=max(tend)
gen estimate=1 if tstat~=.
by studyid: egen nmod=total(estimate), missing

label var meant "Mean"
label var mint "Min."
label var maxt "Max."
label var minyr "Start year"
label var maxyr "End year"
label var nmod "\# models"

estpost tabstat nmod  meant mint maxt minyr maxyr, statistics(mean)  by(studyid) 

esttab . using "table_a1.tex",   cells("nmod  meant(fmt(%9.3f)) mint(fmt(%9.3f)) maxt(fmt(%9.3f)) minyr maxyr") replace ///
	varlabels(`e(labels)') 
	
// Figure 1: Distribution of partial correlations

sort dvshare pc_fe
egen rank_pc=rank(pc_fe), by(dvshare)
label var rank_pc "Partial correlation rank"
gen tcrit=.
forvalues i=58/100 {
	replace tcrit=invttail(`i',0.025) if df==`i'
	}
	
replace tcrit=invttail(101,0.025) if df>100 & df~=.
gen lb=pc_fe-(tcrit*sepcfe)
gen ub=pc_fe+(tcrit*sepcfe)

graph twoway (rspike lb ub rank_pc if dvshare==0, lwidth(thin)) ///
	(scatter pc_fe rank_pc if dvshare==0 & xkey==0, msymbol(o)) ///
	(scatter pc_fe rank_pc if dvshare==0 & xkey==1, mcolor(red) msymbol(t)), ///
	legend(pos(4) ring(0) order(2 3) row(1) label(2 "Control") label(3 "Key")) ///
	yline(0, lcolor(gray) lwidth(thin)) name(g1, replace)  ytitle("Partial correlation") ///
		xlabel(0(10)110)
graph twoway (rspike lb ub rank_pc if dvshare==1, lwidth(thin)) ///
 (scatter pc_fe rank_pc if dvshare==1 & xkey==0, msymbol(o)) ///
	(scatter pc_fe rank_pc if dvshare==1 & xkey==1, mcolor(red) msymbol(t)), ///
	legend(pos(4) ring(0) order(2 3) row(1) label(2 "Control") label(3 "Key")) ///
	yline(0, lcolor(gray)) name(g2, replace)   ytitle("Partial correlation") ///
	xlabel(0(10)120)
	
	
// Table A2: Summary statistics for all variables

local c if dvshare==0  
local x pc_fe sepcfe xkey dvlog develop nonpolity   fectry ldv robust gmm sampyr   year
sum `x'   `c'
sutex `x'   `c', minmax labels file(table_a2_flow) replace digits(2)

local c if dvshare==1  
local x pc_fe sepcfe xkey dvlog develop nonpolity   fectry ldv robust gmm sampyr  year
sum `x'   `c'
sutex `x'   `c', minmax labels file(table_a2_share) replace digits(2)


// Table A3: alternative measure of pub bias



local i insqrtn

// Level 
local c if dvshare==0  
local se c.`i'##i.xkey

reg pc_fe c.`i' i.xkey `c' [aweight=prfesq], robust
est sto m1

reg pc_fe   `se'  `c' [aweight=prfesq], robust
est sto m2

local d  dvlog develop nonpolity   fectry ldv robust gmm sampyr 

reg pc_fe `d' `se'  `c' [aweight=prfesq], robust	
est sto m3



reg pc_fe   `se' year `c' [aweight=prfesq], robust	
est sto m4




// Share
local c if dvshare==1  
local se c.`i'##i.xkey

reg pc_fe c.`i' i.xkey `c' [aweight=prfesq], robust
est sto m5

reg pc_fe   `se'  `c' [aweight=prfesq], robust
est sto m6

local d  dvlog develop nonpolity   fectry ldv robust gmm sampyr 

reg pc_fe `d' `se'  `c' [aweight=prfesq], robust	
est sto m7



reg pc_fe   `se' year `c' [aweight=prfesq], robust	
est sto m8


local mtitles 1 2 3 4 5 6 7 8 

esttab m1 m2 m3 m4 m5 m6 m7 m8 using "tablea3.tex",   ///
	b(%9.3f) se(%9.3f) scalars(r2_a) sfmt(%9.2f) label ///
  compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
  title("Alternative measure of publication bias" \label{tab:pub2}) ///
 mtitles(`mtitles') nobaselevels 

 
log close
